package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.CategoryAttrDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.info.CategoryAttrDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.add.CategoryAttrBatchInsertRequestDTO;
import com.example.child_protection.dto.request.add.CategoryAttrInsertRequestDTO;
import com.example.child_protection.dto.request.update.CategoryAttrUpdateRequestDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.CategoryAttrModel;
import com.example.child_protection.service.CategoryAttrManageService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 类目属性管理service实现层
 * @date 2018-11-2
 * @author Biz
 */
@Component
public class CategoryAttrManageServiceImpl extends MyBaseService implements CategoryAttrManageService {

    @Autowired
    public CategoryAttrDao categoryAttrDao;

    /**
     * 添加
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO insert(CategoryAttrInsertRequestDTO requestDTO){
        return this.service(ServiceActionEnums.CATEGORY_ATTR_INSERT,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<CategoryAttrInsertRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(CategoryAttrInsertRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                CategoryAttrModel model = new CategoryAttrModel();
                BeanUtils.copyProperties(requestDTO,model);
                categoryAttrDao.insert(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 添加批量
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO insertBatch(CategoryAttrBatchInsertRequestDTO requestDTO){
        return this.service(ServiceActionEnums.CATEGORY_ATTR_INSERT,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<CategoryAttrBatchInsertRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(CategoryAttrBatchInsertRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();

                CategoryAttrModel model = new CategoryAttrModel();
                //删出
                model.setCategoryId(requestDTO.getCategoryId());
                model.setLastUpdatePersonId(requestDTO.getLastUpdatePersonId());
                categoryAttrDao.deleteByModel(model);
                //添加
                List<CategoryAttrModel> attrs = JSONObject.parseArray(requestDTO.getAttrValue(), CategoryAttrModel.class);
                for(CategoryAttrModel attr :attrs){
                    model = new CategoryAttrModel();
                    BeanUtils.copyProperties(attr,model);
                    model.setCreatePersonId(requestDTO.getCreatePersonId());
                    model.setLastUpdatePersonId(requestDTO.getLastUpdatePersonId());
                    model.setCategoryId(requestDTO.getCategoryId());
                    categoryAttrDao.insert(model);
                }
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 修改
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO update(CategoryAttrUpdateRequestDTO requestDTO){
        return this.service(ServiceActionEnums.CATEGORY_ATTR_UPDATE,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<CategoryAttrUpdateRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(CategoryAttrUpdateRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                CategoryAttrModel model = new CategoryAttrModel();
                BeanUtils.copyProperties(requestDTO,model);
                categoryAttrDao.updateSelective(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 删除
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO delete(SimpleIdRequestDTO requestDTO){
        return this.service (ServiceActionEnums.CATEGORY_ATTR_DELETE, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                CategoryAttrModel categoryAttrModel = new CategoryAttrModel();
                BeanUtils.copyProperties(requestDTO,categoryAttrModel);
                categoryAttrDao.deleteForLogic(categoryAttrModel);
                res.setId(requestDTO.getId());
                res.successfully();
                return res;
            }
        });
    }

}
